package com.tusuo.stds.ui.level;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.LevelList;
import com.tusuo.stds.rest.LevelRecordList;
import com.tusuo.stds.rest.LevelResult;
import com.tusuo.stds.rest.SceneList;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.main.SceneListAdapter;
import com.tusuo.stds.ui.module.LevelListAdapter;
import com.tusuo.stds.ui.question.LevelRecordDetailActivity_;
import com.tusuo.stds.ui.scene.SceneActivity_;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import at.grabner.circleprogress.CircleProgressView;
import at.grabner.circleprogress.UnitPosition;
import myandroid.library.util.ApplicationHelper;

/**
 * Created by developer on 17-8-30.
 */

@EFragment(R.layout.frag_level_result)
public class LevelResultFragment extends Fragment {
    @ViewById(R.id.frag_level_result_tv_status)
    TextView tvStatus;
    @ViewById(R.id.frag_level_result_tv_error)
    TextView tvError;

    @ViewById(R.id.frag_level_result_layout_result)
    LinearLayout layoutResult;
    @ViewById(R.id.frag_level_result_layout_error)
    RelativeLayout layoutError;


    @ViewById(R.id.frag_level_result_mode_can_pass_layout_star)
    LinearLayout layoutModeCanPassStar;
    @ViewById(R.id.frag_level_result_mode_must_pass_layout_star)
    LinearLayout layoutModeMustPassStar;
    @ViewById(R.id.frag_level_result_mode_must_pass_layout_star2)
    LinearLayout layoutModeMustPassStar2;

    @ViewById(R.id.frag_level_result_iv_star_1)
    ImageView ivStar1;
    @ViewById(R.id.frag_level_result_iv_star_2)
    ImageView ivStar2;
    @ViewById(R.id.frag_level_result_iv_star_3)
    ImageView ivStar3;
    @ViewById(R.id.frag_level_result_circleview)
    CircleProgressView circleProgressView;
    @ViewById(R.id.frag_level_result_tv_clearance_time)
    TextView tvClearanceTime;
    @ViewById(R.id.frag_level_result_tv_persent)
    TextView tvPersent;

    @ViewById(R.id.frag_level_result_layout_only_back)
    LinearLayout layoutOnlyBack;
    @ViewById(R.id.frag_level_result_layout_back_next)
    LinearLayout layoutBackNext;
    @ViewById(R.id.frag_level_result_layout_back_retry)
    LinearLayout layoutBackRetry;

    @ViewById(R.id.frag_level_result_layout_level)
    LinearLayout layoutLevel;
    @ViewById(R.id.frag_list_level_result_level)
    CustomListView listViewLevel;
    @ViewById(R.id.frag_level_result_layout_scene)
    LinearLayout layoutScene;
    @ViewById(R.id.frag_list_level_result_scene)
    CustomListView listViewScene;

    @StringRes(R.string.toolbar_title_level_result)
    String toolbarTitle;
    @StringRes(R.string.err_msg_server)
    String strErrMsgServer;
    @StringRes(R.string.level_result_status_pass)
    String strStatusPass;
    @StringRes(R.string.level_result_status_perfect_pass)
    String strStatusPerfectPass;
    @StringRes(R.string.level_result_status_failed)
    String strStatusFailed;

    @FragmentArg("levelName")
    String mLevelName;
    @FragmentArg("levelId")
    int mLevelRecordId;
    @FragmentArg("levelType")
    int mLevelType;
    @FragmentArg("levelResult")
    LevelResult levelResult;

    private int mNextLevelId = 0;

    @AfterViews
    void initData() {
        // 防止套有ListVew的ScrollView的页面布局默认起始位置不是最顶部
        listViewLevel.setFocusable(false);
        listViewScene.setFocusable(false);

        layoutOnlyBack.setVisibility(View.GONE);
        layoutBackNext.setVisibility(View.GONE);
        layoutBackRetry.setVisibility(View.GONE);

        layoutLevel.setVisibility(View.GONE);
        layoutScene.setVisibility(View.GONE);
        layoutResult.setVisibility(View.GONE);
        layoutError.setVisibility(View.VISIBLE);

        setLevelResult();
    }

    public void setLevelResult() {
        if (levelResult != null) {
            layoutResult.setVisibility(View.VISIBLE);
            layoutError.setVisibility(View.GONE);
        } else {
            layoutResult.setVisibility(View.GONE);
            layoutError.setVisibility(View.VISIBLE);
            return;
        }

        if (levelResult.IsPass) {
            if (levelResult.NextLevelId != 0) {
                // 有下一关
                layoutOnlyBack.setVisibility(View.GONE);
                layoutBackNext.setVisibility(View.VISIBLE);

                mNextLevelId = levelResult.NextLevelId;
            } else {
                layoutOnlyBack.setVisibility(View.VISIBLE);
                layoutBackNext.setVisibility(View.GONE);
            }

            layoutBackRetry.setVisibility(View.GONE);

            if (levelResult.CompletionStatus == 30) {
                tvStatus.setText(strStatusPerfectPass);
            } else {
                tvStatus.setText(strStatusPass);
            }
        } else {
            tvStatus.setText(strStatusFailed);

            layoutOnlyBack.setVisibility(View.GONE);
            layoutBackNext.setVisibility(View.GONE);
            layoutBackRetry.setVisibility(View.VISIBLE);
        }

        if (mLevelType == 1) {
            if (levelResult.IsPass) {
                layoutModeMustPassStar.setVisibility(View.VISIBLE);
                layoutModeMustPassStar2.setVisibility(View.GONE);
            }else {
                layoutModeMustPassStar.setVisibility(View.GONE);
                layoutModeMustPassStar2.setVisibility(View.VISIBLE);
            }

            layoutModeCanPassStar.setVisibility(View.GONE);
        } else if (mLevelType == 10) {
            layoutModeMustPassStar.setVisibility(View.GONE);
            layoutModeCanPassStar.setVisibility(View.VISIBLE);

            switch (levelResult.CompletionStatus) {
                case 100:
                case 0:
                    ivStar1.setImageResource(R.drawable.ic_star_unselected);
                    ivStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 10:
                    ivStar1.setImageResource(R.drawable.ic_star_selected);
                    ivStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 20:
                    ivStar1.setImageResource(R.drawable.ic_star_selected);
                    ivStar2.setImageResource(R.drawable.ic_star_selected);
                    ivStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 30:
                    ivStar1.setImageResource(R.drawable.ic_star_selected);
                    ivStar2.setImageResource(R.drawable.ic_star_selected);
                    ivStar3.setImageResource(R.drawable.ic_star_selected);
                    break;
                default:
                    ivStar1.setImageResource(R.drawable.ic_star_unselected);
                    ivStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
            }
        } else {
            Utils.UI.showToastShort("题型错误！请联系管理员！");
        }

        circleProgressView.setValue(levelResult.CorrectRate * 100);
        tvPersent.setText((int)(levelResult.CorrectRate * 100) + "");
        circleProgressView.setUnitPosition(UnitPosition.RIGHT_BOTTOM);
        tvClearanceTime.setText(getString(R.string.level_clearance_time, Utils.secondsToHumanReadable(levelResult.ClearanceTime)));

        // 设置开启关卡
        if (levelResult.OpenLevelList.size() > 0) {
            layoutLevel.setVisibility(View.VISIBLE);

            List<LevelList.Level> row = new ArrayList<>(2);
            List<List<LevelList.Level>> levelList = new ArrayList<>();
            for (int i = 0; i < levelResult.OpenLevelList.size(); i++) {
                if (i % 2 == 0) {   // left one
                    row = new ArrayList<>(2);

                    row.add(levelResult.OpenLevelList.get(i));

                    // 检查是否是最后一个数据
                    if (i + 1 == levelResult.OpenLevelList.size()) {
                        levelList.add(row);
                    }

                } else {    // right one
                    row.add(levelResult.OpenLevelList.get(i));
                    levelList.add(row);
                }
            }
            LevelListAdapter levelListAdapter = new LevelListAdapter(getActivity(), levelList, false);
            listViewLevel.setAdapter(levelListAdapter);
        } else {
            layoutLevel.setVisibility(View.GONE);
        }

        // 设置开启场景
        if (levelResult.OpenSceneList.size() > 0) {
            layoutScene.setVisibility(View.VISIBLE);

            SceneListAdapter sceneListAdapter = new SceneListAdapter(getActivity(), levelResult.OpenSceneList);
            listViewScene.setAdapter(sceneListAdapter);
        } else {
            layoutScene.setVisibility(View.GONE);
        }

    }

    @ItemClick(R.id.frag_list_level_result_scene)
    void sceneListItemClicked(SceneList.Scene scene) {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        Intent intent = new Intent();
        intent.setClass(getActivity(), SceneActivity_.class);
        intent.putExtra(SceneList.Scene.EXTRA_NAME, Parcels.wrap(scene));
        startActivity(intent);
    }

    @Click(R.id.frag_level_result_btn_retry)
    void retryLevel() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        ApplicationHelper.getInstance().removeActivity(getActivity());
    }

    @Click({R.id.frag_level_result_btn_back_1,
            R.id.frag_level_result_btn_back_2,
            R.id.frag_level_result_btn_only_back})
    void clickBackBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        getActivity().onBackPressed();
    }

    @Click(R.id.frag_level_result_btn_next)
    void clickNextBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        if (mNextLevelId != 0) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), LevelActivity_.class);
            intent.putExtra("levelId", mNextLevelId);
            startActivity(intent);
        } else {
            Utils.UI.showToastShort(strErrMsgServer);
        }
    }

    @Click(R.id.frag_level_result_layout_details)
    void clickRecordDetailsBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        Intent intent = new Intent();
        intent.setClass(getActivity(), LevelRecordDetailActivity_.class);

        LevelRecordList.LevelRecord levelRecord = new LevelRecordList.LevelRecord();
        levelRecord.Id = mLevelRecordId;
        levelRecord.Title = mLevelName;

        intent.putExtra(LevelRecordList.LevelRecord.EXTRA_NAME, Parcels.wrap(levelRecord));
        startActivity(intent);
    }



}
